"""
Tools for generating task sets.
"""

import numpy as np
import random
import math


def UUniFastDiscard(n, u, nsets):
    sets = []
    while len(sets) < nsets:
        # Classic UUniFast algorithm:
        utilizations = []
        sumU = u
        for i in range(1, n):
            nextSumU = sumU * random.random() ** (1.0 / (n - i))
            utilizations.append(sumU - nextSumU)
            sumU = nextSumU
        utilizations.append(sumU)

        # If no task utilization exceeds 1:
        if all(ut <= 1 for ut in utilizations):
            sets.append(utilizations)

    return sets

def Computing_cost(n):
    c_set = []
    for i in range(n):
        c_set.append(random.randint(5,30))
    
    return c_set

def Period(USets,c_set):
    sets = []
    for i in range(len(USets)):
        p_set = []
        for j in range(len(c_set)):
            u_i = USets[i][j]
            p_i = round(c_set[j]/u_i)
            if (p_i >= 50 and p_i % 2 != 0):
                p_set.append(p_i+1)
            else:
                p_set.append(p_i)
        sets.append(p_set)
    return sets

def Deadline(c_set,Psets):
    alpha = 0.5
    sets = []
    for i in range(len(Psets)):
        D_set = []
        for j in range(len(c_set)):
            low_bound = round(c_set[j] + alpha*(Psets[i][j] - c_set[j]))
            up_bound = round(Psets[i][j])
            D_set.append(random.randint(low_bound,up_bound))
        sets.append(D_set)
    return sets


def Upload_cost(c_set,Dsets):
    sets = []
    high_index = 0.7
    low_index = 0.1
    for i in range(len(Dsets)):
        r_set = []
        for j in range(len(c_set)):
            low_bound = round(low_index*(Dsets[i][j] - c_set[j]))
            high_bound = round(high_index*(Dsets[i][j] - c_set[j]))
            r_i = random.randint(low_bound,high_bound)
            r_set.append(r_i)
        sets.append(r_set)
    return sets
